<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" style="width: 500px" class="demo-ruleForm">
      <el-form-item label="品名" prop="name">
        <el-input v-model="form.name" placeholder="请输入品名"></el-input>
      </el-form-item>
      <el-form-item label="产品类型" prop="type1">
        <el-select v-model="form.type1" clearable placeholder="请选择产品类型" @change="queryByName">
          <el-option v-for="item in type1" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型分类" prop="type2">
        <el-select v-model="form.type2" placeholder="请选择类型分类">
          <el-option v-for="item in type2" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="jiage">
        <el-input v-model="form.jiage" placeholder="请输入价格"></el-input>
      </el-form-item>
      <el-form-item label="产品标题" prop="title">
        <el-input type="textarea" v-model="form.title" placeholder="请输入产品标题"></el-input>
      </el-form-item>
      <el-form-item label="规格" prop="guige">
        <el-input v-model="form.guige" placeholder="请输入规格"></el-input>
      </el-form-item>
      <el-form-item label="主料" prop="zhuliao">
        <el-input v-model="form.zhuliao" placeholder="请输入主料"></el-input>
      </el-form-item>
      <el-form-item label="口感" prop="kougan">
        <el-input v-model="form.kougan" placeholder="请输入口感"></el-input>
      </el-form-item>
      <el-form-item label="适应人群" prop="shiyingrenqun">
        <el-input v-model="form.shiyingrenqun" placeholder="请输入适应人群"></el-input>
      </el-form-item>
      <el-form-item label="存储条件" prop="chucuntiaojian">
        <el-input v-model="form.chucuntiaojian" placeholder="请输入存储条件"></el-input>
      </el-form-item>
      <el-form-item label="展示图片" prop="img">
        <el-upload class="avatar-uploader" action :show-file-list="true" :before-upload="beforeAvatarUpload"
          list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img v-if="dialogImageUrl" width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="add">添加商品</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/myaxios";
import ObsClient from "esdk-obs-browserjs/src/obs";

export default {
  data() {
    return {
      imageUrl: "",
      initiateData: {
        option: {},
        uploadId: "",
      },
      fileUrl: "",
      dialogImageUrl: "",
      dialogVisible: false,

      type1: [],
      type2: [],
      num: 0,
      formid: {},
      id: "",
      form: {
        id: "",
        name: "",
        type1: "",
        type2: "",
        jiage: "",
        title: "",
        guige: "",
        zhuliao: "",
        kougan: "",
        shiyingrenqun: "",
        chucuntiaojian: "",
        img: "",
      },
      rules: {
        name: [{ required: true, message: "请输入品名", trigger: "blur" }],
        type1: [
          { required: true, message: "请选择产品类型", trigger: "change" },
        ],
        type2: [
          { required: true, message: "请选择类型分类", trigger: "change" },
        ],
        jiage: [{ required: true, message: "请输入价格", trigger: "blur" },
        {
          pattern:
            /^[0-9]+\.{0,1}[0-9]{0,2}$/,
          message: "请输入正确的价格",
          trigger: "blur",
        },],
        title: [{ required: true, message: "请输入产品标题", trigger: "blur" }],
        guige: [{ required: true, message: "请输入规格", trigger: "blur" }],
        zhuliao: [{ required: true, message: "请输入主料", trigger: "blur" }],
        kougan: [{ required: true, message: "请输入口感", trigger: "blur" }],
        shiyingrenqun: [
          { required: true, message: "请输入适应人群", trigger: "blur" },
        ],
        chucuntiaojian: [
          { required: true, message: "请输入存储条件", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //重置
    resetForm(form) {
      this.$refs["form"].resetFields();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //查询type2数据
    queryByName(params) {
      console.log(params);
      this.type2 = [];
      httpApi.commodity.queryByTypeName({ name: params }).then((res) => {
        for (let i = 0; i <= res.data[0].type.length; i++) {
          this.type2.push(res.data[0].type[i].name);
        }
      });
    },
    //添加操作
    add() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.id = this.id;
          httpApi.commodity.add(this.form).then((res) => {
            if ((res.data.code = 201)) {
              this.$message.success("添加成功");
              // this.$router.push('/home/movie-list')
              console.log(this.form);
            }
            this.$refs["form"].resetFields();
          });
        }
      });
    },

    //图片上传
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      // return isJPG && isLt2M;
      if (isJPG && isLt2M) {
        var obsClient = new ObsClient({
          access_key_id: "CEQDVNAZTLD4SDTQFCTA", // 你的ak
          secret_access_key: "hr4oEqbvC7R0nW6BgqQKI6fA9a6FznVr4W7nGqLM", // 你的sk
          server: "https://obs.cn-north-4.myhuaweicloud.com", // 你的endPoint  记得加入https://
        });
        obsClient.putObject({
          Bucket: "daoxiangcun", // 桶名
          Key: file.name, // 文件名
          SourceFile: file, //流文件
        });
        this.form.img = `https://daoxiangcun.obs.cn-north-4.myhuaweicloud.com/${file.name}`;
      }
    },
  },
  mounted() {
    //查询type1数据
    httpApi.commodity.queryAll().then((res) => {
      console.log("查询所有产品类型", res.data);
      for (let i = 0; i <= res.data.length; i++) {
        this.type1.push(res.data[i].name);
      }
    });
    //让下一个商品的id加1
    httpApi.commodity.queryCPAll().then((res) => {
      let i = res.data.length - 1;
      this.formid = res.data[i];
      this.id = parseInt(this.formid.id) + 1;
      console.log(this.id);
    });

    // console.log(this.form.img);
  },


}
</script>

<style lang="scss" scoped></style>
